package com.example.ui.leftmenu;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

import com.eton.czh.GifView;
import com.example.huashangapp.BaseView;
import com.example.huashangapp.HSApplication;
import com.example.huashangapp.R;
import com.example.net.AnalyzingJson;
import com.example.net.NetRequest;
import com.example.service.MainService;
import com.example.ui.learn.CurriculumCenterActivity;
import com.example.ui.learn.ListeningActivity;
import com.example.ui.learn.util.CurriculumCenter;
import com.example.ui.learn.util.MyListViewAdapter1;
import com.example.ui.learn.util.MyListViewAdapter6;
import com.example.ui.share.ChineseMerchantInterviewActivity;
import com.example.ui.share.HsTalkActivity;
import com.example.util.AsyncLoadImage;
import com.example.util.RefreshableListView;
import com.example.util.RefreshableListView.OnRefreshListener;
import com.example.util.Util;

public class Item04View extends BaseView {
	private Context context;
	private MyListViewAdapter6  item_adapter;
	private ArrayList<CurriculumCenter> item1_cc=new ArrayList<CurriculumCenter>();
	private Button titleLeftBtn,titleRightBtn;
	private TextView titleText;
	private RefreshableListView mListView;
    private String data;	
    HSApplication hsApplication;
    AlertDialog infoad;
	AlertDialog.Builder bulider;
	public Item04View(Context context, HSApplication hsApplication){
		this.context = context;
		this.hsApplication=hsApplication;
	}
	Handler h=new Handler(){
		 public void handleMessage(android.os.Message msg) {
		 switch (msg.what) {
		 case 0:
			 infoad.dismiss();
			 Util.toast(context, "数据提取异常请下拉刷新");
			 break;
		case 1:
			 infoad.dismiss();
			 item_adapter.setCcInfos(item1_cc);
			 item_adapter.notifyDataSetChanged();
			break;
		case 3:
			 infoad.dismiss();
			Util.toast(context, "数据提取异常请下拉刷新");
			break;
		default:
			break;
		}
		 };
		
	};
	Runnable item1_run = new Runnable(){  
		  
		@Override  
		public void run() {  
			
			try {
				 data=NetRequest.Request_Item04();
				 JSONObject j = new JSONObject(data);
				 String result = j.getString("response");
				    if(!result.equals("error")){
				    	 item1_cc=AnalyzingJson.getdata_item4(data);
				    	 HSApplication.item4_cc=item1_cc;
				    	 h.sendEmptyMessage(1);
				    }else{
				    	h.sendEmptyMessage(3);
				    }
			} catch (JSONException e) {
				h.sendEmptyMessage(0);
				e.printStackTrace();
			}
		   
		   
		}  
		  };  
		private  OnClickListener i1_ocl=new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				switch (v.getId()) {
				case R.id.layout_title_left_btn:					
					myMoveView.showHideLeftMenu();
					break;
				case R.id.layout_title_right_btn:
					if(HSApplication.HSlistInfo.getListen_uri()==null||"".equals(HSApplication.HSlistInfo.getListen_uri())){
						Toast.makeText(context, "目前您还没有收听过任何课程!",Toast.LENGTH_SHORT).show();
						return;
					}
					Intent i1_l = new Intent();
					i1_l.setClass(context, ListeningActivity.class);
					Bundle lisinfo=new Bundle();
					lisinfo.putParcelable(ListeningActivity.LISTEN,HSApplication.HSlistInfo);
					i1_l.putExtras(lisinfo);
					i1_l.putExtra(ListeningActivity.ISLISTENING,true);
					context.startActivity(i1_l);
					break;
				default:
					break;
				}
			}
		};
		private GifView mygifView;
	public void getData(){
		mListView=(RefreshableListView)view.findViewById(R.id.item01_listview);
		item_adapter=new MyListViewAdapter6(context,item1_cc,new AsyncLoadImage());
		mListView.setAdapter(item_adapter);
		 mListView.setOnItemClickListener(new OnItemClickListener() {
				@Override
				public void onItemClick(AdapterView<?> arg0, View arg1, int position,
						long arg3) {
					Intent intent = new Intent(context, HsTalkActivity.class);
					intent.putExtra("id", item1_cc.get(position).getId()+"");
					intent.putExtra("title", item1_cc.get(position).getTitle());
					context.startActivity(intent);
				}
			});
			mListView.setOnRefreshListener(new OnRefreshListener() {
				@Override
				public void onRefresh(RefreshableListView listView) {
					new NewDataTask().execute();
					
				}
			});
		if(HSApplication.item4_cc.size()>0){
			item1_cc=HSApplication.item4_cc;
			 item_adapter.setCcInfos(item1_cc);
			 item_adapter.notifyDataSetChanged();
		}else{
			bulider = new AlertDialog.Builder(context);
			bulider.setView(View.inflate(context, R.layout.onloading, null))
					.create();
			infoad = bulider.show();
			new Thread(item1_run).start();  
		}
	}
	public void init(){
		view = LayoutInflater.from(context).inflate(R.layout.item01, null);
		titleLeftBtn = (Button)view.findViewById(R.id.layout_title_left_btn);
		titleRightBtn= (Button) view.findViewById(R.id.layout_title_right_btn);
		titleText=(TextView)view.findViewById(R.id.layout_title_middle_text);
		titleLeftBtn.setText("菜单");
		titleText.setText("华商访谈");
		titleRightBtn.setText("正在听");
		Resources r = context.getResources(); 
		int btn_h = (int) r.getDimension(R.dimen.ting_left);
		int btn_left=(int)r.getDimension(R.dimen.text_left);
		titleRightBtn.setPadding(btn_left, 0,  btn_h, 0);
		mygifView = (GifView)view.findViewById(R.id.testView);
		mygifView.setGifImage(R.drawable.ting);
		mygifView.setVisibility(0);
		titleLeftBtn.setOnClickListener(i1_ocl);
		titleRightBtn.setOnClickListener(i1_ocl);
		//初始化数据
		getData();
	}
	private class NewDataTask extends AsyncTask<Void, Void, String> {
        @Override
        protected String doInBackground(Void... params) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {}
            
			return null;
        }
        @Override
        protected void onPostExecute(String result) {                   	
        	bulider = new AlertDialog.Builder(context);
			bulider.setView(View.inflate(context, R.layout.onloading, null))
					.create();
			infoad = bulider.show();
			new Thread(item1_run).start();  
            mListView.completeRefreshing();        	
            super.onPostExecute(result);
        }
    }

	
	
	

	
	
}
